<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Created by JetBrains PhpStorm.
 * User: xx
 * Date: 13-12-12
 * Time: 下午5:24
 * To change this template use File | Settings | File Templates.
 */

class pipeline extends XX_Controller
{
    public function __construct()
    {
        //调用原有的构造函数
        parent::__construct();

        $this->load->library('Schedule');
        $this->load->model('work_model', 'workMod');
        $this->load->model('pipeline_model', 'pipeMod');
    }

    public function index()
    {
        $pipe_arr = array();
        $pipe_name = $this->input->get_post('pipe_name', TRUE);
        $ret = $this->pipeMod->get_pipe_data();
        $pipe_list = array();

        if ($ret->get_code() === 0) {
            $pipe_list = $ret->get_data();
            foreach ($pipe_list as $pipe) {
                $pipe_arr[$pipe['pipe_name']] = $pipe['pipe_title'];
            }
        }

        if (!$pipe_name) {
            $pipe = current($pipe_list);
            $pipe_name = $pipe['pipe_name']; //默认第一个
        }

        $pipedb = $this->get_pipe($pipe_name, $pipe_arr);

        //  print_r($pipedb);

        $this->load->helper('language');
        $this->load->view('pipeline/pipeline_index', array('pipe_list' => $pipe_list, 'pipe_name' => $pipe_name, 'pipedb' => $pipedb));
    }

    public function manage()
    {
        $ret = $this->pipeMod->get_pipe_data();
        $pipe_list = array();
        if ($ret->get_code() === 0) {
            $pipe_list = $ret->get_data();
            foreach ($pipe_list as $pipe) {
                $pipe_arr[$pipe['pipe_name']] = $pipe['pipe_title'];
            }
        }
        $this->load->helper('language');
        $this->load->view('pipeline/pipeline_manage', array('pipe_list' => $pipe_list));
    }

    public function change_status()
    {
        $pipe_id = $this->input->post('pipe_id', TRUE);
        $pipe_status = $this->input->post('pipe_status', TRUE);

        $this->pipeMod->edit_pipe($pipe_id, array('pipe_status' => $pipe_status));
        echo 'ok';
    }

    public function adjust_pipe()
    {
        $work_code = $this->input->post('work_code', TRUE);
        $from = $this->input->post('from', TRUE);
        $to = $this->input->post('to', TRUE);
        $this->schedule->pl_move($work_code, $from, $to);
    }

    public function edit_pipe()
    {
        $pipe_id = $this->input->get('pipe_id', TRUE);
        $ret = $this->pipeMod->get_pipe($pipe_id);
        $pipe = array();
        if ($ret->get_code() === 0) {
            $pipe = $ret->get_data();
        }

        $this->load->helper('language');
        $this->load->view('pipeline/pipeline_edit', array('pipe' => $pipe));
    }

    public function edit_pipe_do()
    {
        $pipe_id = $this->input->post('pipe_id', TRUE);
        $this->pipeMod->edit_pipe($pipe_id, $this->input->post());
        $this->manage();
    }

    /**
     * 操作员看板
     */
    public function signboard()
    {
        $pipe_arr = array();
        $ret = $this->pipeMod->get_pipe_data();
        if ($ret->get_code() === 0) {
            $pipe_data = $ret->get_data();
            foreach ($pipe_data as $pipe) {
                $pipe_arr[$pipe['pipe_name']] = $pipe['pipe_title'];
            }
        }

        $pname = $this->input->get('pipe_name', TRUE);
        $pipe = $this->get_pipe($pname, $pipe_arr);
        $other_pipe_statistica = $this->fun_other_pipe_statistica($pname, $pipe_arr); // 其它流水线情况

        //已发货
        $deliver_goods = $this->workMod->deliver_goods();
        $working = $this->get_working();


        $weekarray = array("日", "一", "二", "三", "四", "五", "六");
        $this->load->helper('language');
        $this->load->view('pipeline/pipeline_signboard', array(
            'pipe' => $pipe,
            'week' => $weekarray[date("w")],
            'statistica' => $this->workMod->statistica_detailed(),
            'other_pipe_statistica' => $other_pipe_statistica,
            'deliver_goods' => $deliver_goods,
            'working' => $working,
            'pipe_arr' => $pipe_arr
        ));
    }

    /**
     * @param $pname
     * @param $pipe_arr
     */
    function fun_other_pipe_statistica($pname, $pipe_arr)
    {
        $other_pipe = '';
        foreach ($this->schedule->pipeline['db'] as $pipe_name => $pipe_node) {
            if ($pname != $pipe_name) {
                $a = $pipe_node['count'];
                $b = 0;
                $c = 0;
                $statistical = $this->workMod->get_statistical($pname);
                $c = $statistical[$pname];
                if (isset($this->schedule->hang_up_list['db'][$pipe_name])) {
                    $hang_up = $this->schedule->hang_up_list['db'][$pipe_name];
                    $b = $hang_up['count'];
                }
                $other_pipe .= '流水线' . $pipe_arr[$pipe_name] . '：<b>正在分拣' . $a . '，&nbsp;缺货分拣' . $b . '，&nbsp;今日已分拣' . $c . ';</b>&nbsp;&nbsp;';
            }
        }

        return $other_pipe;
    }

    function get_working()
    {
        $working_list = array();
        foreach ($this->schedule->pipeline['db'] as $pipe_name => $pipe_node) {
            foreach ($pipe_node['index_db'] as $work) {
                if ($work && $work['node_code'] && isset($this->schedule->code_list[$work['node_code']])) {
                    $cl_node = $this->schedule->code_list[$work['node_code']];
                    $ret = $this->workMod->get_work_data($cl_node['work_id']);
                    $work_data = $ret->get_data();
                    $working_list[$work_data['work_counter']] = array('work_counter' => $work_data['work_counter'], 'flag' => 0);
                }
            }

            $hang_up = false;
            if (isset($this->schedule->hang_up_list['db'][$pipe_name])) {
                $hang_up = $this->schedule->hang_up_list['db'][$pipe_name];
            }
            if ($hang_up) {
                $pipe['hang_up_count'] = $hang_up['count']; //挂起长
                foreach ($hang_up['index_db'] as $hang_up_node) {
                    if ($hang_up_node && isset($hang_up_node['node_code']) && isset($this->schedule->code_list[$hang_up_node['node_code']])) {
                        $cl_node = $this->schedule->code_list[$hang_up_node['node_code']];
                        $ret = $this->workMod->get_work_data($cl_node['work_id']);
                        if ($ret->get_code() === 0) {
                            $work_data = $ret->get_data();
                            $working_list[$work_data['work_counter']] = array('work_counter' => $work_data['work_counter'], 'flag' => 1);
                        }
                    }
                }
            }
        }
        krsort($working_list);//排序
        return $working_list;
    }

    /**
     * 获取pipe
     * @param $pname
     * @param $pipe_arr
     * @return array
     */
    function get_pipe($pname, $pipe_arr)
    {
        $pipe = array();

        $statistical = $this->workMod->get_statistical($pname);
        $pipe['all_count'] = $statistical['all'];
        $pipe['self_count'] = $statistical[$pname];
        $pipe['name'] = $pname;
        $pipe['title'] = $pipe_arr[$pname];

        foreach ($this->schedule->pipeline['db'] as $pipe_name => $pipe_node) {
            if ($pname == $pipe_name) {
                $pipe['status'] = $pipe_node['status'];
                $pipe['pipeline_count'] = $pipe_node['count']; //流水线长
                $pipe['wl_count'] = $this->schedule->work_list['count']; //排队数
                $pipe['list'] = array();
                $pipe['hang_up'] = array();
                foreach ($pipe_node['index_db'] as $work) {
                    if ($work && $work['node_code'] && isset($this->schedule->code_list[$work['node_code']])) {
                        $cl_node = $this->schedule->code_list[$work['node_code']];
                        $ret = $this->workMod->get_work_data($cl_node['work_id']);
                        if ($ret->get_code() === 0) {
                            $work_data = $ret->get_data();
                            //$work_data['use_time'] = $work['cyle_time'] - $work['use_time'];
                            $work_data['use_time'] = $this->get_time_str($work['use_time']);
                            $work_data['use_time_original'] = $work['use_time'];
                            $pipe['list'][] = $work_data;
                        }
                    }
                }

                $hang_up = false;
                if (isset($this->schedule->hang_up_list['db'][$pipe_name])) {
                    $hang_up = $this->schedule->hang_up_list['db'][$pipe_name];
                }
                if ($hang_up) {
                    $pipe['hang_up_count'] = $hang_up['count']; //挂起长
                    foreach ($hang_up['index_db'] as $hang_up_node) {
                        if ($hang_up_node && isset($hang_up_node['node_code']) && isset($this->schedule->code_list[$hang_up_node['node_code']])) {
                            $cl_node = $this->schedule->code_list[$hang_up_node['node_code']];
                            $ret = $this->workMod->get_work_data($cl_node['work_id']);
                            if ($ret->get_code() === 0) {
                                $work_data = $ret->get_data();
                                $work_data['hu_time'] = $this->get_time_str($hang_up_node['hu_time']);
                                $work_data['hu_time_original'] = $hang_up_node['hu_time'];
                                $pipe['hang_up'][] = $work_data;
                            }
                        }
                    }
                }
            }
        }
        $pipe['p_h_count'] = $this->schedule->pipeline['work_count'] + $this->schedule->hang_up_list['work_count']; //挂起长
        $pipe['mark'] = $this->get_pipe_mark($pname);
        return $pipe;
    }

    function check_pipe_mark()
    {
        $pname = $this->input->get('pipe_name', TRUE);
        $old_mark = $this->input->get('old_mark', TRUE);
        $mark = $this->get_pipe_mark($pname);
        echo $mark == $old_mark ? 'TRUE' : "FALSE";
    }

    /**
     * 获取流水线状态标记,用于判断流水线是否有增删改变
     * 状态标记： md5([node_code][node_code]...)
     * @param $pname
     * @return string
     */
    function get_pipe_mark($pname)
    {
        $mark = '';
        foreach ($this->schedule->pipeline['db'] as $pipe_name => $pipe_node) {
            if ($pname == $pipe_name) {
                foreach ($pipe_node['index_db'] as $work) {
                    $mark .= '[' . $work['node_code'] . ']';
                }

                $hang_up = false;
                if (isset($this->schedule->hang_up_list['db'][$pipe_name])) {
                    $hang_up = $this->schedule->hang_up_list['db'][$pipe_name];
                }
                if ($hang_up) {
                    foreach ($hang_up['index_db'] as $hang_up_node) {
                        if ($hang_up_node) {
                            $mark .= '[' . $hang_up_node['node_code'] . ']';
                        }
                    }
                }
            }
        }

        return md5($mark);
    }

    function get_time_str($time)
    {
        $s = $time % 60;
        $s = "0" . $s;
        $m = floor($time / 60);
        $m = "0" . $m;
        $str = substr($m, -2, 2) . ':' . substr($s, -2, 2);
        return $str;
    }
}